package com.wanxi.step07test.service.impl;

import com.wanxi.step07test.model.Permission;
import com.wanxi.step07test.model.SUser;
import com.wanxi.step07test.service.SUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

@Service
public class MyUserDetailsService implements UserDetailsService {
    @Autowired
    SUserService sUserService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //获取当前登录用户的数据库数据
        SUser user= sUserService.getSUserByName(username);
        //根据用户id获取当前用户的权限信息
        List<Permission> permissionList = sUserService.getPermissionsBySUserId(user.getId());
        HashSet<Permission> permissions = new HashSet<>(permissionList);
        //将权限信息设置到user对象里。
        user.setAuthorities(permissions);
        return user;
    }



}
